<?php include "menu.phtml";

// breadcrumb design
$breadcrumbs = null;
foreach($this->breadcrumbs as $one) {
	$name = ucfirst($one['name'] == 'root' ? 'home' : $one['name']);
	$breadcrumbs .= "<a href='{$this->url(array('browse' => $one['id']))}'>{$name}</a> / ";
}

?>
<div id="right">
	<div class="rightbox">
		<div class="context"><div class="in"><a href="javascript:void(0);" id="context_menu" class="icon_link the_down"><span></span></a></div></div>
		<h1><?php echo $this->translate->_("Pet Files"); ?>: <?php echo substr($breadcrumbs, 0, -2); ?></h1>
		<?php if(isset($this->up['errors']) && count($this->up['errors']) > 0):?>
			<br />
			<div class="c_error">
				<?php foreach($this->up['errors'] as $idx => $error): ?>
					<div><b><?php echo $idx; ?></b></div>
					<ul>
						<?php foreach($error as $item): ?>
							<li><?php echo $item; ?></li>
						<?php endforeach; ?>
					</ul>
				<?php endforeach; ?>
			</div>
		<?php endif; ?>
		<?php if(isset($this->up['success']) && count($this->up['success']) > 0):?>
			<br />
			<div class="c_success">
				<ul>
					<?php foreach($this->up['success'] as $idx => $success): ?>
						<li><?php echo $idx; ?> <?php echo $this->translate->_("was successfully uploaded"); ?>.</li>
					<?php endforeach; ?>
				</ul>
			</div>
		<?php endif; ?>
		<br />
		<?php echo $this->paginationControl($this->browse, 'Elastic', 'pagination-controls.phtml', array('pos' => 'top')); ?>
		<table cellspacing="0" cellpadding="5" class="grid">
		<?php if($this->admin): ?><col width="23" /><col width="26" /><col /><col width="80" /><col width="200" /><col width="130" /><col width="160" />
		<?php else: ?><col width="26" /><col /><col width="80" /><col width="200" /><col width="1" /><?php endif; ?>
			<tr>
				<?php if($this->admin): ?><th><input type="checkbox" class="master" style="width: auto; margin: 0px;"/></th><?php endif; ?>
				<th></th>
				<th><a href="<?php echo $this->url(array('order' => 'name', 'dir' => $this->rdir)); ?>"><?php echo $this->translate->_("Name"); ?><?php if($this->order == 'name'): ?>&nbsp;<img src="/images/order/<?php echo $this->dir; ?>.png" /><?php endif; ?></a></th>
				<th style="text-align:right;"><a href="<?php echo $this->url(array('order' => 'size', 'dir' => $this->rdir)); ?>"><?php echo $this->translate->_("Size"); ?><?php if($this->order == 'size'): ?>&nbsp;<img src="/images/order/<?php echo $this->dir; ?>.png" /><?php endif; ?></a></th>
				<th style="text-align:center;"><a href="<?php echo $this->url(array('order' => 'modified', 'dir' => $this->rdir)); ?>"><?php echo $this->translate->_("Modified"); ?><?php if($this->order == 'modified'): ?>&nbsp;<img src="/images/order/<?php echo $this->dir; ?>.png" /><?php endif; ?></a></th>
				<?php if($this->admin): ?>
					<th><a href="<?php echo $this->url(array('order' => 'rights', 'dir' => $this->rdir)); ?>"><?php echo $this->translate->_("Access Rights"); ?><?php if($this->order == 'rights'): ?>&nbsp;<img src="/images/order/<?php echo $this->dir; ?>.png" /><?php endif; ?></a></th>
				<?php endif; ?>
				<th></th>
			</tr>
			<?php if($this->root->getParentId() != 0): ?>
				<tr>
					<?php if($this->admin): ?><td><input type="checkbox" disabled="disabled" class="slave" rel="folder|<?php echo $this->root->getParentId(); ?>" style="width: auto; margin: 0px;"/></td><?php endif; ?>
					<td><div style="width: 16px; height: 16px;"><img src="/images/files/dir.gif" alt="#" style="display: block;" /></div></td>
					<td><div style="width: 20px;"><a class="ellipsis" href="<?php echo $this->url(array('controller'=>'pets', 'action'=>'files', 'pet' => $this->pet->getId(), 'browse' => $this->root->getParentId(), 'order' => $this->order, 'dir' => $this->dir), 'default', true); ?>" title="Up">...</a></div></td>
					<td align="right"></td>
					<td align="center"></td>
					<td colspan="2"></td>
				</tr>
			<?php endif; if(isset($this->browse) && count($this->browse) > 0): foreach($this->browse as $item): ?>
				<?php
					// file link
					if($item['type'] == 'dir') $link = array('controller'=>'pets', 'action'=>'files', 'pet' => $this->pet->getId(), 'browse' => $item['id'], 'order' => $this->order, 'dir' => $this->dir);
					else $link = array('controller'=>'pets', 'action'=>'files', 'download'=> $item['id']);

					// edit link
					if($item['type'] == 'dir') $edit = array('controller'=>'pets', 'action'=>'files', 'pet' => $this->pet->getId(), 'browse' => $this->root->getId(), 'permission' => $item['id'], 'order' => $this->order, 'dir' => $this->dir);
					else $edit = array('controller'=>'pets', 'action'=>'files', 'pet' => $this->pet->getId(), 'browse' => $this->root->getId(), 'access' => $item['id'], 'order' => $this->order, 'dir' => $this->dir);

					// delete link
					if($item['type'] == 'dir') $delete = array('controller'=>'pets', 'action'=>'files', 'delete' => $item['id']);
					else $delete = array('controller'=>'pets', 'action'=>'files', 'remove' => $item['id']);

					// dont allow rights edit if gallery or video
					$gov = false;

					// gallery
					if(($this->root->getName() == 'root' && $item['name'] == 'gallery') || $this->root->getName() == 'gallery')
						$gov = true;

					// videos
					if(($this->root->getName() == 'root' && $item['name'] == 'videos') || $this->root->getName() == 'videos')
						$gov = true;

					// audios
					if(($this->root->getName() == 'root' && $item['name'] == 'audios') || $this->root->getName() == 'audios')
						$gov = true;
				?>
				<tr>
					<?php if($this->admin): ?><td><input type="checkbox" class="slave" rel="<?php echo $item['type'] == 'dir' ? 'folder' : 'file'; ?>|<?php echo $item['id']; ?>" style="width: auto; margin: 0px;"/></td><?php endif; ?>
					<td><div style="width: 16px; height: 16px;"><img src="<?php echo $item['ico']; ?>" alt="#" style="display: block;" /></div></td>
					<td><div style="width: <?php echo $this->admin ? 213 : 496; ?>px; overflow: hidden;"><a class="ellipsis" href="<?php echo $this->url($link, 'default', true); ?>" title="<?php echo $item['name']; ?>"><?php echo $item['name']; ?></a></div></td>
					<td align="right"><?php echo $item['type'] == 'yt' ? '1 KB' : $item['size']; ?></td>
					<td align="center"><?php echo Petolio_Service_Util::formatDate($item['date'], Petolio_Service_Util::LONGDATE, true, true); ?></td>
					<?php if($this->admin): ?>
						<td><?php echo $item['rights']; ?></td>
						<td align="right" nowrap="nowrap">
							<?php if($gov): ?>
								<a class="icon_link disabled edit"><span><?php echo $this->translate->_("Edit Rights"); ?></span></a>
							<?php else: ?>
								<a href="<?php echo $this->url($edit, 'default', true); ?>" class="icon_link edit"><span><?php echo $this->translate->_("Edit Rights"); ?></span></a>
							<?php endif; if($this->admin == $item['owner']): ?>
								<a href="<?php echo $this->url($delete, 'default', true); ?>" class="icon_link delete reqconf" title="<?php echo $this->translate->_("Are you sure you want to delete this ?"); ?>"><span><?php echo $this->translate->_("Delete"); ?></span></a>
							<?php else: ?>
								<a class="icon_link disabled delete"><span><?php echo $this->translate->_("Delete"); ?></span></a>
							<?php endif; ?>
						</td>
					<?php else: ?>
						<td></td>
					<?php endif; ?>
				</tr>
			<?php endforeach; else: ?>
				<tr>
					<?php if($this->admin): ?><td>&nbsp;</td><?php endif; ?>
					<td><img src="/images/files/file.gif" alt="#" style="display: block;" /></td>
					<td colspan="5" class="red"><?php echo $this->translate->_("Nothing here"); ?>.</td>
				</tr>
			<?php endif; if($this->admin): ?>
				<tr style="background: #F8F8F8;">
					<td><img src="/images/link.png" alt="file-link" style="display: block;" /></td>
					<td colspan="6" class="grey" style="padding-top: 7px;">
						<?php printf($this->translate->_("%s Items Selected"), "<span id='sel'>0</span>"); ?>.
						<span id="links" class="grey">
							<a href="javascript:void(0);" id="sel_edit" class="icon_link edit"><span><?php echo $this->translate->_("Edit Rights"); ?></span></a>
							<a href="javascript:void(0);" id="sel_delete" class="icon_link delete"><span><?php echo $this->translate->_("Delete"); ?></span></a>
						</span>
					</td>
				</tr>
			<?php endif; ?>
		</table>
		<?php echo $this->paginationControl($this->browse, 'Elastic', 'pagination-controls.phtml', array('pos' => 'bot')); ?>
		<?php if(isset($this->form)): ?>
			<hr />
			<div class="cls"></div>
			<?php echo $this->form; ?>
		<?php endif; ?>
		<?php if(isset($this->rights)): ?>
			<hr />
			<div class="cls"></div>
			<form action="" method="post">
				<div><label class="checkbox" for="item_0"><input type="radio" id="item_0" name="access_value" value="0" <?php if($this->rights['access'] == 0): ?>checked="checked"<?php endif; ?>><span><?php echo $this->translate->_("Private"); ?></span></label><div class="cls"></div></div>
				<div><label class="checkbox" for="item_1"><input type="radio" id="item_1" name="access_value" value="1" <?php if($this->rights['access'] == 1): ?>checked="checked"<?php endif; ?>><span><?php echo $this->translate->_("Everyone"); ?></span></label><div class="cls"></div></div>
				<div>
					<table cellpadding="0" cellspacing="0"><col width="265" /><tr><td valign="top"><label class="checkbox" for="item_2"><input type="radio" id="item_2" name="access_value" value="2" <?php if($this->rights['access'] == 2): ?>checked="checked"<?php endif; ?>><span><?php echo $this->translate->_("Link Partners"); ?></span></label><div class="cls"></div></td>
					<td valign="top"><div id="multi_item_2">
					<select title="<?php echo $this->translate->_("All Link Partners"); ?>" name="access_partners[]" class="chzn-select chzn-custom" multiple="multiple" style="width:390px;" id="multi_partners">
						<?php foreach($this->partners as $idx => $user): ?>
							<option value="<?php echo $idx; ?>" <?php if($this->rights['access'] == 2 && in_array($idx, $this->rights['users_id'])): ?>selected="selected"<?php endif; ?>><?php echo $user['name']; ?></option>
						<?php endforeach; ?>
					</select>
					</div>
					</td></tr></table>
				</div>
				<div>
					<table cellpadding="0" cellspacing="0"><col width="265" /><tr><td valign="top"><label class="checkbox" for="item_3"><input type="radio" id="item_3" name="access_value" value="3" <?php if($this->rights['access'] == 3): ?>checked="checked"<?php endif; ?>><span><?php echo $this->translate->_("Friends"); ?></span></label><div class="cls"></div></td>
					<td valign="top"><div id="multi_item_3">
					<select title="<?php echo $this->translate->_("All Friends"); ?>" name="access_friends[]" class="chzn-select chzn-custom" multiple="multiple" style="width:390px;" id="multi_friends">
						<?php foreach($this->friends as $idx => $user): ?>
							<option value="<?php echo $idx; ?>" <?php if($this->rights['access'] == 3 && in_array($idx, $this->rights['users_id'])): ?>selected="selected"<?php endif; ?>><?php echo $user['name']; ?></option>
						<?php endforeach; ?>
					</select>
					</div>
					</td></tr></table>
				</div>
				<div><table cellpadding="0" cellspacing="0"><col width="265" /><tr><td valign="top"><label class="checkbox" for="item_4"><input type="radio" id="item_4" name="access_value" value="4" <?php if($this->rights['access'] == 4): ?>checked="checked"<?php endif; ?>><span><?php echo $this->translate->_("Users"); ?></span></label><div class="cls"></div></td>
					<td valign="top"><div id="multi_item_4">
					<input type="text" title="<?php echo $this->translate->_("Select Users"); ?>" name="access_users" class="chzn-select chzn-custom" multiple="multiple" style="width:380px;" id="multi_users" value="<?php echo $this->rights['access'] == 4 ? $this->rights['users_id_name'] : null; ?>">
					</div>
					</td></tr></table><div class="cls"></div>
				</div>
				<div><label for="submit">&nbsp;</label><input type="submit" value="<?php echo $this->translate->_("Save Access Rights >"); ?>" id="submit" name="submit"><div class="cls"></div></div>
				<?php if($this->mass_selection): ?><input type="hidden" name="mass_selection" value="<?php echo $this->mass_selection; ?>" /><?php endif; ?>
				<?php if($this->mass_action): ?><input type="hidden" name="mass_action" value="<?php echo $this->mass_action; ?>" /><?php endif; ?>
			</form>
		<?php endif; if($this->admin): ?>
			<hr />
			<div class="left"><input onclick="Petolio.go('<?php echo $this->url(array('controller'=>'pets', 'action'=>'files', 'pet' => $this->pet->getId(), 'browse' => $this->root->getId(), 'upload' => 'folder', 'order' => $this->order, 'dir' => $this->dir), 'default', true); ?>');" type="button" value="<?php echo $this->translate->_("Create Folder >"); ?>" id="submit" name="prev" style="margin: 0px;"><div class="clear"></div></div>
			<?php if($this->root->getName() == 'gallery'): ?>
				<div class="right"><input onclick="Petolio.go('<?php echo $this->url(array('controller'=>'pets', 'action'=>'pictures', 'pet'=>$this->pet->getId()), 'default', true); ?>');" type="button" value="<?php echo $this->translate->_("Upload Pictures >"); ?>" id="submit" name="next"><div class="clear"></div></div>
			<?php elseif($this->root->getName() == 'audios'): ?>
				<div class="right"><input onclick="Petolio.go('<?php echo $this->url(array('controller'=>'pets', 'action'=>'audios', 'pet'=>$this->pet->getId()), 'default', true); ?>');" type="button" value="<?php echo $this->translate->_("Upload Audios >"); ?>" id="submit" name="next"><div class="clear"></div></div>
			<?php elseif($this->root->getName() == 'videos'): ?>
				<div class="right"><input onclick="Petolio.go('<?php echo $this->url(array('controller'=>'pets', 'action'=>'videos', 'pet'=>$this->pet->getId()), 'default', true); ?>');" type="button" value="<?php echo $this->translate->_("Upload Videos >"); ?>" id="submit" name="next"><div class="clear"></div></div>
			<?php else: ?>
				<div class="right"><input onclick="Petolio.go('<?php echo $this->url(array('controller'=>'pets', 'action'=>'files', 'pet' => $this->pet->getId(), 'browse' => $this->root->getId(), 'upload' => 'file', 'order' => $this->order, 'dir' => $this->dir), 'default', true); ?>');" type="button" value="<?php echo $this->translate->_("Upload Files >"); ?>" id="submit" name="next"><div class="clear"></div></div>
			<?php endif; ?>
		<?php endif; ?>
		<div class="clear"></div>
	</div>
</div>
<script type="text/javascript">
	var ChznUsr = function() {
		var error = <?php echo isset($this->users_error) ? 'true' : 'false'; ?>;

		function showError() {
			$("#multi_item_4").find('ul.chzn-choices').css({ borderColor: 'red' });
		};

		function enable(i) {
			var p = $(i).attr('id'),
				w = $('div[id="multi_'+ p +'_block"]');

			if(w.length)
				w.remove();
		};

		function disable(i, s) {
			var p = $(i).attr('id'),
				a = $('div[id="multi_'+ p +'"]'),
				w = $('div[id="multi_'+ p +'_block"]'),
				z = 500 - s;

			if(!a.length || w.length)
				return;

			a.append('<div id="multi_'+ p +'_block" style="position: absolute; margin-top: -2px; width: 100%; height: 100%; background: white; opacity: 0.6; top: 0px; left: 0px; z-index: '+ z +';" />');
		};

		function decide(e) {
			$.each($('input[type="radio"]'), function(s, i){
				if(i.checked) enable(i);
				else disable(i, s + 1 * s);
			});
		};

		function post(u, p) {
		    var f = ['<form method="POST" action="'+ u +'">'];

		    f.push('<input type="hidden" name="mass_action" value="'+ p.mass_action +'"/>');
		    $.each(p.folder, function(k, v) { f.push('<input type="hidden" name="mass_folders[]" value="'+ v +'"/>'); });
		    $.each(p.file, function(k, v) { f.push('<input type="hidden" name="mass_files[]" value="'+ v +'"/>'); });
		    f.push('</form>');

		    $(f.join('')).appendTo('body').get(0).submit();
		};

		function selection() {
			var a = $('#sel'),
				m = $('input[class="master"]'),
				s = $('input[class="slave"]'),
				b = $('#sel_edit'),
				c = $('#sel_delete'),
				d = $('input[name="mass_selection"]');

			if(d.length > 0) {
				$.each(d.val().split(','), function(s, i){
					$('input[rel="'+ i +'"]').get(0).checked = true;
				});

				__doit();
			}

			b.click(__send);
			c.click(__send);
			s.click(__doit);
			m.click(function(){
				__doit(function(i){
					if(!i.disabled)
						i.checked = m.get(0).checked;
				});
			});

			function __send(e) {
				var x = 0,
					w = [],
					i = $(e.currentTarget),
					d = {
						mass_action: i.attr('id'),
						folder: [],
						file: []
					};

				s.each(function(s, i) {
					if(i.checked){
						w = $(i).attr('rel').split('|');
						d[w[0]].push(w[1]);
						x++;
					}
				});

				if(x > 0)
					post(document.URL, d);
			};

			function __doit(f) {
				var x = 0,
					l = $('#links');

				s.each(function(s, i) {
					if(typeof(f) == 'function')
						f.call(this, i);
					if(i.checked)
						x++;
				});
				a.html(x);

				if(x > 0) l.removeClass('grey');
				else l.addClass('grey');
			};
		};

		function chooser() {
			var x = $(".chzn-select");
			$.each(x, function(s, i){
				var y = $(i),
					o = {translate: Petolio.translateChosen()},
					a = {ajax: {
		            	url: SITE_URL + "pets/find-user",
						min_search: 3,
						req_param: "user",
						res_value: "value",
						res_text: "text",
						res_html: "text",
						success_callback: function(v){
							return v.results;
						}
		            }};

				if(y.attr('title'))
					y.data('placeholder', y.attr('title'));

				if(y.is("input"))
					$.extend(o, a);

				y.chosen(o);
			});

			$('td > div[id*="multi_"]').css({ position: 'relative', marginTop: '-1px' });
			$("#multi_partners_chzn").css({ zIndex: '495' });
			$("#multi_friends_chzn").css({ zIndex: '493' });
			$("#multi_users_chzn").css({ zIndex: '491' });
		};

		function bind() {
			decide();
			$('input[type="radio"]').click(decide);
		};

		function __construct() {
			selection();
			chooser();
			bind();
			if(error)
				showError();
		};

		return {
			init: __construct
		};
	}();

	READY(ChznUsr.init);
</script>